﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Pb;
public class ClubListener : Game
{
    public GameObject Root;
    EginDN dn = EginDN.Instance;
    EginUser user = EginUser.Instance;

    private static ClubListener instance;

    public static ClubListener Instance
    {
        get
        {
            return instance;
        }
    }
    public int _clubid = 0;
    public int _clubNum = 0;
    void Awake()
    {
        instance = this;
    }
    void Start()
    {

        base.StartGameSocket();
        SocketManager.Instance.socketListener = this;
        //Debug.LogError("ClubListenr>>>>>>>");
        //NnRoomListRequest req = new NnRoomListRequest();
        //req.GroundType = NN_GROUND_TYPE.Club;
        //req.Page
    }
    public override void SocketReceiveMessage(byte[] data)
    {

        base.SocketReceiveMessage(data);
        byte[] dataLength = data.Skip(0).Take(2).ToArray();
        byte[] dataId = data.Skip(2).Take(4).ToArray();
        byte[] dataConcent = data.Skip(data.Length - (data.Length - 4)).ToArray();
        int TempMsgID = ProtoDataToMsg.Byte2ushort(dataId);

        //加入俱乐部返回
        //if (TempMsgID == MsgId.NnJoinClubResponse)
        //{
        //    NnJoinClubResponse resp = NnJoinClubResponse.Parser.ParseFrom(dataConcent);
        //    Debug.Log("消息ID" + TempMsgID + "消息内容" + resp);
        //    if (resp.Status == STATUS.Success)
        //    {
        //        user.logMsg = "申请加入俱乐部成功~";
        //        GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        //    }
        //    else
        //    {
        //        user.logMsg = resp.Msg;
        //        GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        //    }
        //}
        ////获取俱乐部成员
        //else if (TempMsgID == MsgId.NnClubMembersResponse)
        //{
        //    NnClubMembersResponse resp = NnClubMembersResponse.Parser.ParseFrom(dataConcent);
        //    Debug.Log("消息ID" + TempMsgID + "消息内容" + resp);
        //    if (resp.Members.Count == 0)
        //    {

        //        user.logMsg = "没有更多~";
        //        GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));

        //        if (ClubhuizhangModel.Instance != null)
        //        {

        //            ClubhuizhangModel.Instance.ShowNowState();
        //        }
        //    }
        //    else
        //    {
        //        if (resp.Members[0].Id == int.Parse(user.Uid))//会长
        //        {
        //            if (ClubhuizhangModel.Instance == null)
        //            {
        //                GameObject obj = GameObject.Instantiate(Resources.Load("Club/ClubhuizhangPanul") as GameObject);
        //                obj.name = "ClubhuizhangPanul";
        //                obj.transform.SetParent(Root.transform);
        //                obj.transform.localScale = Vector3.one;
        //                obj.transform.localPosition = Vector3.zero;
        //                obj.transform.localRotation = Quaternion.identity;

        //            }

        //            ClubhuizhangModel.Instance.JibenInit(resp);
        //        }
        //        else
        //        {
        //            if (ClubChengyuanModel.Instance == null)
        //            {
        //                GameObject obj = GameObject.Instantiate(Resources.Load("Club/ClubchengyuanPanul") as GameObject);
        //                obj.name = "ClubchengyuanPanul";
        //                obj.transform.SetParent(Root.transform);
        //                obj.transform.localScale = Vector3.one;
        //                obj.transform.localPosition = Vector3.zero;
        //                obj.transform.localRotation = Quaternion.identity;
        //            }
        //            ClubChengyuanModel.Instance.Init(resp);
        //        }
        //    }
        //}
        ////获取自己的俱乐部名称
        //else if (TempMsgID == MsgId.NnClubListResponse)
        //{
        //    NnClubListResponse resp = NnClubListResponse.Parser.ParseFrom(dataConcent);
        //    if (resp.Clubs.Count == 0)
        //    {
        //        GameObject obj = GameObject.Instantiate(Resources.Load("Club/JoinClub") as GameObject);
        //        obj.name = "JoinClub";
        //        obj.transform.SetParent(Root.transform);
        //        obj.transform.localScale = Vector3.one;
        //        obj.transform.localPosition = Vector3.zero;
        //        obj.transform.localRotation = Quaternion.identity;
        //    }
        //    else
        //    {
        //        for (int i = 0; i < resp.Clubs.Count; i++)
        //        {
        //            _clubid = resp.Clubs[i].Id;
        //            _clubNum = resp.Clubs[i].MemberCount;
        //            Debug.LogError("--------" + _clubid + "=------" + _clubNum);
        //        }
        //        //第一次打开显示第一页
        //        NnClubMembersRequest req = new NnClubMembersRequest();
        //        req.ClubId = _clubid;
        //        req.Page = 1;
        //        SocketManager sm = SocketManager.Instance;
        //        sm.SendProtobuf1(req, MsgId.NnClubMembersRequest);
        //    }

        //}
        ////创建俱乐部应答
        //else if (TempMsgID == MsgId.NnCreateClubResponse)
        //{
        //    NnCreateClubResponse resp = NnCreateClubResponse.Parser.ParseFrom(dataConcent);
        //    if (resp.Status == STATUS.Success)
        //    {
        //        user.logMsg = "恭喜创建成功~";
        //        GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));

        //    }
        //    else
        //    {
        //        user.logMsg = resp.Msg;
        //        GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        //    }
        //}
        ////获取俱乐部牛牛房间应答
        //else if (TempMsgID == MsgId.NnRoomListResponse)
        //{
        //    NnRoomListResponse resp = NnRoomListResponse.Parser.ParseFrom(dataConcent);
        //    if (ClubhuizhangModel.Instance)//会长加载
        //    {
        //        if (resp.Status == STATUS.Success)
        //        {
        //            try
        //            {
        //                ClubhuizhangModel.Instance.KaifangInit(resp);
        //            }
        //            catch (System.Exception)
        //            {
        //                Debug.LogError("ClubListenter----146行，异常(Instance失败)");
        //            }
        //        }
        //        else
        //        {
        //            user.logMsg = resp.Msg;
        //            GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        //        }
        //    }
        //    else//更新主场景俱乐部房间
        //    {
        //        Debug.LogError("---NnRoomListResponse---");

        //        Root.transform.Find("HallPanel").GetComponent<HallModel>().SetClubRoomList(resp);
        //    }
         
            
        //}
        ////获取房间规则应答
        //else if (TempMsgID == MsgId.NnRoomOptionsResponse)
        //{
        //    Debug.LogError("------NnRoomOptionsResponse--------");
        //    NnRoomOptionsResponse resp = NnRoomOptionsResponse.Parser.ParseFrom(dataConcent);
        //    try
        //    {
        //        ClubhuizhangModel.Instance.KaiFangJieShaoInit(resp);
        //    }
        //    catch (System.Exception)
        //    {
        //        Debug.LogError("ClubListenter----165行，异常(Instance失败)");
        //    }
        //}
        //////俱乐部创建房间应答(暂时没用到，俱乐部创建房间在HallModel----NNOpenRoom)
        ////else if (TempMsgID == MsgId.NnCreateRoomResponse)
        ////{
        ////    NnCreateRoomResponse resp = NnCreateRoomResponse.Parser.ParseFrom(dataConcent);
        ////    if (resp.Status == STATUS.Success)
        ////    {
        ////        user.logMsg = "创建房间成功~";
        ////        GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        ////    }
        ////    else
        ////    {
        ////        user.logMsg = resp.Msg;
        ////        GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        ////    }
        ////}
        ////获取俱乐部入会申请的列表
        //else if (TempMsgID == MsgId.NnReviewingMemberListResponse)
        //{
        //    NnReviewingMemberListResponse resp = NnReviewingMemberListResponse.Parser.ParseFrom(dataConcent);
        //    if (ClubhuizhangModel.Instance != null)
        //    {
        //        ClubhuizhangModel.Instance.RuHuiInit(resp);
        //    }

        //}
        //else if (TempMsgID == MsgId.NnHandleMemberResponse)
        //{
        //    NnHandleMemberResponse resp = NnHandleMemberResponse.Parser.ParseFrom(dataConcent);

        //    if (resp.Status == STATUS.Success)
        //    {
        //        //界面没有关闭
        //        if (ClubhuizhangModel.Instance != null)
        //        {
        //            if (ClubhuizhangModel.Instance._currentState == ButtonState.JiBenState)
        //            {
        //                user.logMsg = "踢出成功";
        //                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        //            }
        //            else
        //            {
        //                user.logMsg = "操作成功";
        //                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        //            }

        //        }
        //    }
        //    else
        //    {
        //        user.logMsg = resp.Msg;
        //        GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        //    }
        //}
        //else if (TempMsgID == MsgId.NnHandleMemberBroadcast)
        //{
        //    NnHandleMemberBroadcast resp = NnHandleMemberBroadcast.Parser.ParseFrom(dataConcent);
        //    user.logMsg = "您被移除俱乐部~";
        //    GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
        //}
        //else if (TempMsgID == MsgId.NnOpenRoomResponse)
        //{
        //    Pb.NnOpenRoomResponse newpb = Pb.NnOpenRoomResponse.Parser.ParseFrom(dataConcent);
        //    Debug.LogError("消息ID" + TempMsgID + "消息内容" + newpb);
        //    if (newpb.Status == STATUS.Success)
        //    {
        //        if (newpb.SeatId == 0)
        //        {
        //            NnRoomListRequest req = new NnRoomListRequest();
        //            req.GroundType = NN_GROUND_TYPE.Club;
        //            req.Page = 1;
        //            SocketManager sm = SocketManager.Instance;
        //            sm.SendProtobuf1(req, MsgId.NnRoomListRequest);
        //        }
        //        else
        //        {
        //            dn.createrUid = newpb.RoomInfo.CreaterUid;
        //            dn.roomId = newpb.RoomInfo.RoomId;
        //            dn.totalRound = newpb.RoomInfo.TotalRound;
        //            dn.seatId = newpb.SeatId;
        //            dn.roomStatus = newpb.RoomInfo.RoomStatus;
        //            Debug.Log(dn.seatId + "hall");
        //            user.gameUserInfoList.Add(newpb.UserInfo);

        //            //soundModel.SwitchBgMusic(user.gameType);
        //            Application.LoadLevelAsync(ScenePath.GameDNPlay);
        //        }


        //    }
        //}
    }
}
